<?php
/**
 * 消息模型
 * 
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0 alpha
 * @since 2011-05-04
 * @author monkee
 */

class feedModel extends model
{
	
	/**
	 * 发布feed消息
	 * @param int $user_id 		产生feed的用户ID
	 * @param string $message 	动态信息
	 * @param int model_id 		相关类型的ID
	 */
	public function add($user_id, $message, $model_id){
		$user_ids = $this->get_follows($user_id);
		foreach($user_ids as $item){
			$data = array('user_id'=>$item,'message'=>$message,'model_id'=>$model_id,'mktime'=>TIMESTAMP);
			$feed_id = DB::insert('feed', $data);
		}
		return $feed_id;
	}
	
	/**
	 * 根据消息ID删除记录
	 * @param int $feed_id	消息ID
	 */
	public function delete($feed_id){
		$condition = "feed_id=$feed_id";
		$row = DB::delete('feed',$condition);
		if($row>0) return 1;
		else return 0;
	}
	/**
	 * 根据消息ID获取记录
	 * @param int $feed_id	消息ID
	 */
	public function get_feed_by_feed_id($feed_id){
		$row = DB::fetchOne("SELECT * FROM ".DB::table('feed')." WHERE feed_id=$feed_id");
		if(count($row) == 0){
			return array();
		}
		return $row;
	}
	/**
	 * 根据消息ID获取其后面的记录
	 * @param int $feed_id	消息ID
	 * @param int $count	获取的个数
	 */
	public function get_feeds_by_pro_id($feed_id,$count){
		$r = DB::fetchArray("SELECT * FROM ".DB::table('feed')." WHERE feed_id>$feed_id limit $count");
		return $r;
	}
	/**
	 * 根据消息ID获取其后面的记录数
	 * @param int $feed_id	消息ID
	 */
	public function get_feeds_count_by_pro_id($feed_id){
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('feed')." WHERE feed_id>$feed_id");
		return $row['num'];
	}
	
	/**
	 * 分页获取消息列表
	 * @param int $user_id 用户id
	 * @param int $start 起始ID号 排序是倒序排序
	 * @param int $count 记录条数
	 * 
	 * @return 二维数组
	 */
	public function get_feeds_by_user_id($user_id, $start = 0, $count = 30){
		$count = intval($count);
		$count == 0 && $count = 30;
		$sql = 'SELECT a.*, b.*  FROM '.DB::table('feed').' as a,'.DB::table('model').' as b WHERE a.user_id = '.$user_id.' AND a.model_id = b.model_id';
		if($start > 0){
			$sql .= ' AND feed_id < '.$start;
		}
		$sql .= ' ORDER BY feed_id DESC LIMIT '.$count;
		$row = DB::fetchArray($sql);
		return $row;
	}
	/**
	 * 获取所有消息数
	 * 
	 * @return int
	 */
	public function get_feeds_count(){
		$row = DB::fetchOne("SELECT count(*) as num FROM ".DB::table('feed'));
		return $row['num'];
	}
	public function get_model_id_by_type($type, $relate_id){
		$row = DB::fetchOne('SELECT model_id FROM '.DB::table('model').' WHERE type = "'.DB::escape_string($type).'" AND relate_id = '.$relate_id.' LIMIT 1');
		return intval($row['model_id']);
	}
	
	/**
	 * 发布feed消息
	 * //$type (doing|forum|work|share) 类型
	 * 
	 * @param int $user_id 产生feed的用户ID
	 * @param string $username 产生feed的用户名
	 * @param string $message 动态信息
	 * @param int model_id 相关类型的ID
	 */
	/*
	public function publish($user_id, $username, $message, $model_id){
		$users = $this->get_follows($user_id);
		$arr = array();
		$time = TIMESTAMP;
		foreach($users as $i){
			$arr[] = "({$user_id}, '{$i}, {$time}, '".DB::escape_string($message)."', {$model_id})";
		}
		$sql = 'INSERT INTO '.DB::table('feed').' (user_id, mktime, message, model_id) VALUES ';
		$sql .= implode(',', $arr);
		DB::query($sql);
		return 0;
	}
	
	public function get_feed($user_id, $start = 0){
		$sql = 'SELECT * FROM '.DB::table('feed')." WHERE user_id = {$user_id}";
		if($start >0 ){
			$sql .= " AND feed_id < {$start}";
		}
		$sql .= ' ORDER BY feed_id DESC LIMIT 20';
		return DB::fetchArray($sql);
	}
	
	public function comment($feed_id){
		
	}
	
	public function get_feed_by_id($feed_id){
		return DB::fetchOne('SELECT * FROM '.DB::table('feed')." WHERE feed_id = {$feed_id}");
	}
	*/
	public function get_follows($user_id){
		$row = DB::fetchArray('SELECT user_id FROM '.DB::table('user'));
		$data = array();
		foreach($row as $i){
			$data[] = $i['user_id'];
		}
		return $data;
	}
	
}
?>
